package com.jd.jstable.strategy;

import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenyipu on 2018/4/10.
 */
public class ResourceKey implements Serializable{

    private String clazz;
    private String method;
    private List<Object> args =new ArrayList<>();

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public List<Object> getArgs() {
        return args;
    }

    public void setArgs(List<Object> args) {
        this.args = args;
    }

    public ResourceKey(String clazz, String method, List<Object> args) {
        this.clazz = clazz;
        this.method = method;
        this.args = args;
    }

    public String toEasyString(){
        return String.format("%s-%s-%s", clazz, method, combineArrayEasily(args));
    }

    private String combineArrayEasily(List<Object> args) {
        StringBuilder sb = new StringBuilder();
        for (Object arg : args) {
            if(!StringUtils.isEmpty(sb.toString())){
                sb.append('|');
            }
            if(arg instanceof String || arg instanceof Integer || arg instanceof Double
                    || arg instanceof Long || arg instanceof Character){
                sb.append(arg);
            }
            else {
                sb.append(JSON.toJSONString(arg));
            }

        }
        return sb.toString();
    }
}
